package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface DishMapper {
    /**
     * 根据条件统计菜品数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);

    /**
     * 根据分类id查询菜品数量
     * @param categoryId
     * @return
     */
//    @Select("select count(id) from dish where category_id = #{categoryId}")
    Integer countByCategoryId(Long categoryId);

    /**
     * 新增菜品
     * @param dishDTO
     */
    @AutoFill(OperationType.INSERT)
    void addDish(Dish dishDTO);

    /**
     * 分页查询菜品
     * @param dishDTO
     * @return
     */
    Page<DishVO> pageQuery(DishPageQueryDTO dishDTO);

    /**
     * 先判断菜品有没有被套餐关联
     * @param ids
     * @return
     */
    int isMealJoin(List<Long> ids);

    /**
     * 菜品有无在售状态
     * @param ids
     * @return
     */
    int isStatusUsing(List<Long> ids);

    /**
     * 删除菜品关联所有口味
     * @param id
     */
    @Delete("delete from dish_flavor where dish_id = #{id}")
    void DishFlavorMapper(Long id);

    /**
     * 根据id删除所有菜品
     * @param id
     */
    @Delete("delete from dish where id = #{id}")
    void deleteDishByIds(Long id);

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Select("select d.* ,c.name category_name from dish d left JOIN category c on d.category_id = c.id where d.id = #{id}")
    DishVO getById(Long id);

    /**
     * 根据id修改菜品
     * @param dish
     */
    @AutoFill(OperationType.UPDATE)
    void update(Dish dish);

    /**
     * 查询菜品列表
     * @param categoryId
     * @return
     */
//    @Select("select * from dish where category_id = #{categoryId} and status = 1")
    List<DishVO> selectDishList(@Param("categoryId") Long categoryId,@Param("name") String name);

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    List<DishVO> list(Dish dish);
}
